package cn.iocoder.yudao.module.knowledge.controller.admin.item;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.knowledge.controller.admin.item.vo.*;
import cn.iocoder.yudao.module.knowledge.dal.dataobject.item.ItemDetailDO;
import cn.iocoder.yudao.module.knowledge.service.item.ItemDetailService;

@Tag(name = "管理后台 - 知识库明细")
@RestController
@RequestMapping("/knowledge/base-tree-detail")
@Validated
public class BaseTreeDetailController {

    @Resource
    private ItemDetailService itemDetailService;

    @PutMapping("/update")
    @Operation(summary = "更新知识库明细")
    @PreAuthorize("@ss.hasPermission('knowledge:base-tree-detail:update')")
    public CommonResult<Boolean> saveBaseTreeDetail(@Valid @RequestBody ItemDetailSaveReqVO createReqVO) {
        itemDetailService.saveBaseTreeDetail(createReqVO);
        return success(true);
    }


    @DeleteMapping("/delete")
    @Operation(summary = "删除知识库明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('knowledge:base-tree-detail:delete')")
    public CommonResult<Boolean> deleteBaseTreeDetail(@RequestParam("id") Long id) {
        itemDetailService.deleteBaseTreeDetail(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除知识库明细")
    @PreAuthorize("@ss.hasPermission('knowledge:base-tree-detail:delete')")
    public CommonResult<Boolean> deleteBaseTreeDetailList(@RequestParam("ids") List<Long> ids) {
        itemDetailService.deleteBaseTreeDetailListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得知识库明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // 知识库开放给所有人 不需要权限
//    @PreAuthorize("@ss.hasPermission('knowledge:base-tree-detail:query')")
    public CommonResult<ItemDetailRespVO> getBaseTreeDetail(@RequestParam("id") Long id) {
        ItemDetailDO baseTreeDetail = itemDetailService.getBaseTreeDetail(id);
        return success(BeanUtils.toBean(baseTreeDetail, ItemDetailRespVO.class));
    }

    // 不需要分页查询
//    @GetMapping("/page")
//    @Operation(summary = "获得知识库明细分页")
//    @PreAuthorize("@ss.hasPermission('knowledge:base-tree-detail:query')")
//    public CommonResult<PageResult<ItemDetailRespVO>> getBaseTreeDetailPage(@Valid ItemDetailPageReqVO pageReqVO) {
//        PageResult<ItemDetailDO> pageResult = itemDetailService.getBaseTreeDetailPage(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, ItemDetailRespVO.class));
//    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出知识库明细 Excel")
    @PreAuthorize("@ss.hasPermission('knowledge:base-tree-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBaseTreeDetailExcel(@Valid ItemDetailPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ItemDetailDO> list = itemDetailService.getBaseTreeDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "知识库明细.xls", "数据", ItemDetailRespVO.class,
                BeanUtils.toBean(list, ItemDetailRespVO.class));
    }

}